﻿using CustomModel;
using DeskInterfaceBLL;
using HeyPiPi_Desk.Base;
using HeyPiPi_Desk.Views;
using IDeskServiceBLL;
using LibrarySystem.Tools;
using MaterialDesignThemes.Wpf;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Utility;

namespace HeyPiPi_Desk.DomainViewModel
{
    public class LoginDialogViewModel: BaseNotification
    {

        #region 字段/属性

        private readonly IAuthBLL authBLL = new AuthBLL();

        private readonly IShopBLL shopBLL = new ShopBLL(); 

        /// <summary>
        /// 打开对话框命令
        /// </summary>
        public DelegateCommand OpenShopDialog_Cmd { get; set; }

        /// <summary>
        /// 关闭程序命令
        /// </summary>
        public DelegateCommand CloseApp_Cmd { get; set; }

        private string _phone;

        /// <summary>
        /// 用户手机号码
        /// </summary>
        public string Phone
        {
            get { return _phone; }
            set { _phone = value;this.RaisePropertyChanged(() => this.Phone); }
        }

        /// <summary>
        /// 登陆命令
        /// </summary>
        public DelegateCommand Login_Cmd { get; set; }

        /// <summary>
        /// 密码控件
        /// </summary>
        public PasswordBox Pwdbox{ get; set; }


        #endregion


        #region 私有方法

        /// <summary>
        /// 初始化命令
        /// </summary>
        private void _initFunc()
        {
            ///打开注册对话框命令
            this.OpenShopDialog_Cmd = new DelegateCommand(() =>
            {
                DialogHost.CloseDialogCommand.Execute(null, null);
                ShopRegistView shopRegistView = new ShopRegistView();
                shopRegistView.ShowDialog();
            });
            ///登陆命令
            this.Login_Cmd = new DelegateCommand(() =>
            {
                // todo:跳过登陆加快测试
                DialogHost.CloseDialogCommand.Execute(null, null);

                // 通过用户信息获得用户信息
                if (string.IsNullOrEmpty(this.Phone))
                {
                    GlobalSetting.GetGlobalSetting().OpenTipMessage("手机号码不能为空");
                    //MessageBox.Show("手机号码不能为空", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }

                if (!CommonHelper.ValidateRegex(this.Phone,CommonHelper.Phone_Reg))
                {
                    GlobalSetting.GetGlobalSetting().OpenTipMessage("手机号码格式不对");
                    //MessageBox.Show("手机号码格式不对", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                if (string.IsNullOrEmpty(this.Pwdbox.Password))
                {
                    GlobalSetting.GetGlobalSetting().OpenTipMessage("密码不能为空");
                    //MessageBox.Show("密码不能为空", "警告", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
                pet_user pet_User = this.authBLL.Login(this.Phone, CommonHelper.Encryption(this.Pwdbox.Password), out this.error);
                if (this.error==null)
                {
                    GlobalParam.user = pet_User;

                    // 关闭自己
                    DialogHost.CloseDialogCommand.Execute(null, null);
                }
                else
                {
                    MessageBox.Show(this.error);
                    this.error = null;
                }
           


            });
            //关闭程序命令
            this.CloseApp_Cmd = new DelegateCommand(() =>
            {
                Application.Current.MainWindow.Close();
            });
        }

        #endregion


        #region 共有方法

        public LoginDialogViewModel()
        {
            this._initFunc();
        }

        #endregion

    }
}
